package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.addMessage(guestbook);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public List<Object> queryInfo() {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        List<Object> list=null;
        try {
           list= guestbookDao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteBean(Integer id) {
            DBManager dbManager = DBManager.getInstance();
            //连接数据库
            Connection conn = dbManager.getConnection();
            GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
            //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
            guestbookDao.setConnection(conn);
            boolean isRight = false;
            try {
                isRight = guestbookDao.deleteById(id);
                //提交事物
                conn.commit();
            } catch (SQLException e) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                e.printStackTrace();
            }
            return isRight;
        }

    @Override
    public boolean update(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.changeMessage(gb);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }


    @Override
    public List<Object> getById(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        List<Object> list=null;

            list= guestbookDao.getQueryById(id);

        return list;
    }
}
